define(function (require, exports, module) {
    var angular = require('angular');
    require("pagination");
    require("angular-ui");
    require('/public/js/myservice/contract/contract-common');
    var myMath = require('/public/js/myservice/common/my-math');
    var util = require("util");
    var api_prefix = G_CONFIG["api_prefix"]; //api前缀

    angular.module('ContractBalance', [])
        .controller('contractBalanceCtrl', ['$scope', '$routeParams', 'contractCommonService', function ($scope, $routeParams, contractCommonService) {
            $scope.balanceInfo = {};
            var contractId = $routeParams.id;
            //获取合同信息
            contractCommonService.getContract(contractId, function (data) {
                $scope.contractBaseInfo = data.contract;
                $scope.$applyAsync();
            })

            //获取结算返款信息
            contractCommonService.getContractBalanceInfo(contractId, function (data) {
                $scope.balanceInfo = data;
                $scope.balanceInfo.balance_rate = myMath.mul($scope.balanceInfo.balance_rate, 100);
                $scope.$applyAsync();
            })

            // 客户下拉框处理
            $scope.ddlConfigBalanceType = {
                allowClear: false,
                data: [],
                placeholder: '结算类型'
            };
            //结算类型 0：标准结算 1：特价结算 2：特批其他
            $scope.ddlConfigBalanceType.data = [
                {id: 0, text: '标准结算'},
                {id: 1, text: '特价结算'},
                {id: 2, text: '特批其他'},
                {id: 3, text: '折上折'}];

            //保存结算信息
            $scope.save = function () {
                var balanceInfo = angular.copy($scope.balanceInfo);
                balanceInfo.balance_rate = myMath.div(balanceInfo.balance_rate, 100);

                var post_data = balanceInfo;
                var url = api_prefix + "/contract/update-contract-account";

                util.myAjax({
                    url: url,
                    type: "POST",
                    context: this,
                    data: post_data,
                    success: function (json) {
                        if (json.retCode === "0") {
                            util.alert('保存成功');
                        } else {
                            util.alert("保存失败：" + json.errMsg);
                        }
                    }
                });
            }
        }]);
});